<?php
//處理buy傳遞來的參數 檢查生產訂單

include("common.inc.php");
InitGP(array("action","gid","oid","num","quantity")); //初始化變量全局返回
InitGP(array("locals"));	
InitGP(array("quantity0","gid0","quantity1","gid1","quantity2","gid2","quantity3","gid3")); //for cake order

checklogin();//檢查是否登錄

$payment="buysafe";//默認支付方式
$delivery="1";//默認配送方式
//格式化支付方式
include_once CFG_CACHEPATH.'/sys_pay.cache.php';
$payment_list = array();
foreach($payment_select as $k => $val)
{
	$temp_arr['name'] = $cfg_pay_info['name'][$k];
	$temp_arr['logo'] = 'images/pay/'.$cfg_pay_info['logo'][$k];
	$temp_arr['des']	= $cfg_pay_info['des'][$k];
	$temp_arr['value'] = $val;
	$temp_arr['exp'] = sprintf("%01.2f", $price*$payment_exp[$k]);
	$payment_list[] = $temp_arr;
}
//初始化運送方式對象
include(INC_PATH."/delivery.class.php");
$dl=new DeliveryClass();
$dataarray=$dl->getdata(); //獲取數據

//紀錄訂單明細
$orderdetail="";
$orderdetail.=GetNum($gid0)."-".GetNum($quantity0).";";
$orderdetail.=GetNum($gid1)."-".GetNum($quantity1).";";
$orderdetail.=GetNum($gid2)."-".GetNum($quantity2).";";
$orderdetail.=GetNum($gid3)."-".GetNum($quantity3).";";



//當已下訂單付款時
if (GetNum($oid)) {
	//如果存在oid
	include(INC_PATH."/order.class.php"); //包含團購類
	$o=new OrderClass();
	$ordervalue=$o->getone($oid);
	if (is_array($ordervalue)) {
		$value['title']=$ordervalue['gtitle'];
		$num=$ordervalue['num'];
		$gid=$ordervalue['gid'];		
	
		$order_total_price=$ordervalue['order_total_price'];
		$order_incharge=$ordervalue['order_incharge'];
		$payment=$ordervalue['payment'];
		$delivery=$ordervalue['delivery'];
		$order_weight=$ordervalue['order_weight'];
		$fname=$ordervalue['fname'];
		$address=$ordervalue['address'];
		$zip=$ordervalue['zip'];
		$tel =$ordervalue['tel'];
		$tmp =$ordervalue['tmp'];
		/* 自訂發票欄位 */
		$txtftitle =$ordervalue['txtftitle'];
		$txtfidnum =$ordervalue['txtfidnum'];
		$txtfname =$ordervalue['txtfname'];
		$txtfname =$ordervalue['txtfname'];
		$txtftel =$ordervalue['txtftel'];
		$txtfaddress =$ordervalue['txtfaddress'];
		$txtfps =$ordervalue['txtfps'];
		
		
	}else showmessage("指定訂單ID不存在","-1",false);
}
	//還未下訂單時
	if (GetNum($quantity0)) {
		$num=$quantity0;	
	
	}

	
	if(GetNum($gid) || GetNum($num)){
		include(INC_PATH."/group.class.php"); //包含團購類
		$gp=new GroupClass();
		$value=$gp->getOneGroup($gid); 	//獲取團購信息
		
		//判斷能否團購
		if($value[0]['group_end_time']< $timestamp ||$value[0]['group_buynum']>= $value[0]['stock'])
		showmessage("該團購已經結束","-1",false);
		include_once(INC_PATH."/order.class.php");
		$o=new OrderClass();		
		$userBuyCount=$o->getsum($_USERS['uname'],$gid);
		
		//需要判斷
		$x = 0;
		$totalPrice = 0;
		$totalNum = 0;
		while($value[$x]){
			//echo GetNum($quantity.$x);
			if(($userBuyCount+$num) > $value[$x]['max_buynum'])showmessage("您的購買數量超出了單個用戶最大購買數！","index.php",false);
			$value[$x]['num']=$_GET['quantity'.$x];
			$totalNum += $value[$x]['num'];
			$x++;

		}
		$totalPrice = $totalNum * $value[0]['shop_price'];
		//價格格式化
		$price = sprintf("%01.2f", $price);
		//包含支付方式配置文件
		
//	print_r($dataarray);
//	print_r($value);
//	print_r($payment_list);

	}else showmessage("缺少參數","-1",false);
	include template('checkorder');//包含輸出指定模板


?>